package com.sp.homework;

import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * 要求: 使用BufferedReader读取一个文本文件, 为每行加上行号
 * 再连同内容一并输出到屏幕上
 *
 * 扩展: 如果把文件的编码改成了gbk, 出现中文乱码, 思考如何解决
 * 1. 按照utf-8处理, 开始没有乱码
 * 2. 提示: 使用我们的转换流, 将FileInputStream->InputStreamReader->BufferedReader
 */
public class Homework02 {

    //第二题
    @Test
    public void m1() throws IOException {
        String filePath = "d:\\development\\code\\IO_Test\\news\\homework02.txt";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
        String line = "";
        int lineNum = 0;

        while((line = bufferedReader.readLine()) != null){
            System.out.println(++lineNum + line);
        }
        bufferedReader.close();
    }

    //扩展
    @Test
    public void m2() throws IOException {
        String filePath = "d:\\development\\code\\IO_Test\\news\\homework02.txt";
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(filePath), "gbk");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "gbk"));
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        inputStreamReader.close();
    }
}
